package gateway.model.converter;

import cn.hutool.core.collection.CollectionUtil;
import com.google.common.collect.Lists;
import common.model.converter.CommonSysAuthorityConverter;
import common.model.dto.LoginToken;
import common.model.enums.RoleIsAdminEnum;
import common.model.enums.SysAuthorityTypeEnum;
import common.model.po.SysAuthority;
import common.model.po.SysRole;
import common.model.po.SysUser;
import common.model.vo.ButtonVo;
import common.util.RouteUtil;
import gateway.model.dto.SecurityUser;
import gateway.model.vo.UserVo;

import java.util.List;
import java.util.Optional;

/**
 * @author 米泽鹏
 * @since 2022-3-19 上午 3:25
 */
public class SecurityConverter {

	public static SecurityUser loginTokenToSecurityUser(LoginToken loginToken) {
		SecurityUser securityUser = new SecurityUser();
		SysUser user = new SysUser();
		user.setId(loginToken.getUserId());
		user.setNickname(loginToken.getNickname());
		user.setUsername(loginToken.getUsername());
		securityUser.setUser(user);
		loginToken.getRoleIdSet().stream().map(roleId -> {
			SysRole role = new SysRole();
			role.setId(roleId);
			return role;
		}).forEach(role -> securityUser.getRoleList().add(role));
		loginToken.getAuthorityIdSet().stream().map(authorityId -> {
			SysAuthority authority = new SysAuthority();
			authority.setId(authorityId);
			return authority;
		}).forEach(authority -> securityUser.getAuthorityList().add(authority));
		return securityUser;
	}

	public static UserVo securityUserToUserVo(SecurityUser securityUser) {
		UserVo userVo = new UserVo();
		userVo.setUserId(securityUser.getUser().getId());
		userVo.setToken(securityUser.getToken());
		userVo.setUsername(securityUser.getUsername());
		userVo.setNickname(securityUser.getUser().getNickname());
		userVo.setIsAdmin(securityUser.getRoleList().stream().anyMatch(role -> RoleIsAdminEnum.IS_ADMIN.getValue().equals(role.getIsAdmin())));
		userVo.setEmail(securityUser.getUser().getEmail());
		userVo.setAvatar(securityUser.getUser().getAvatar());
		userVo.setGender(securityUser.getUser().getGender());
		userVo.setIntroduction(securityUser.getUser().getIntroduction());
		userVo.setEmailNotifyTypes(Optional.ofNullable(securityUser.getUser().getEmailNotifyTypes()).orElse(Lists.newArrayList()));
		userVo.setLastModifyUsernameDate(securityUser.getUser().getLastModifyUsernameDate());
		userVo.setLastModifyNicknameDate(securityUser.getUser().getLastModifyNicknameDate());
		userVo.setWebConfig(securityUser.getWebConfig());
		userVo.setExpire(securityUser.getExpire());
		List<SysRole> roleList = securityUser.getRoleList();
		roleList.forEach(userRole -> {
			userVo.getRoleCodes().add(userRole.getRoleCode());
			userVo.getRoleNames().add(userRole.getRoleName());
		});
		List<SysAuthority> authorityList = securityUser.getAuthorityList();
		if (CollectionUtil.isNotEmpty(authorityList)) {
			List<SysAuthority> authorityMenuList = Lists.newArrayList();
			authorityList.forEach(authority -> {
				if (SysAuthorityTypeEnum.MENU.getValue().equals(authority.getType()) || SysAuthorityTypeEnum.STRUCTURE.getValue().equals(authority.getType())) {
					authorityMenuList.add(authority);
				}
				if (SysAuthorityTypeEnum.BUTTON.getValue().equals(authority.getType())) {
					ButtonVo buttonVO = CommonSysAuthorityConverter.poToButtonVO(authority);
					userVo.getButtons().add(buttonVO);
				}
			});
			userVo.setMenus(RouteUtil.buildRoutes(authorityMenuList));
		}
		return userVo;
	}

}
